#region Using

using System;
using System.IO;
using System.Web;
using System.Net;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BlogEngine.Core;

#endregion


namespace Controls
{
	/// <summary>
	/// Creates and displays a photo album based on various settings.
	/// </summary>
	public class PhotoAlbum : WebControl
	{
		private List<Photo> _PhotoList = new List<Photo>();
		private Guid _RelTag = Guid.NewGuid();

		/// <summary>
		/// Standard Constructor.  
		/// </summary>
		public PhotoAlbum()
			: base()
		{
		}

		private int _MaxPhotoSize = -1;
		[BindableAttribute(true)]
		[CategoryAttribute("Appearance")]
		public int MaximumPhotoSize
		{
			get { return _MaxPhotoSize == -1 ? BlogSettings.Instance.SlideShowPhotoSize : _MaxPhotoSize; }
			set { _MaxPhotoSize = value; }
		}

		private int _MaxPerPage = 0;
		[BindableAttribute(true)]
		[CategoryAttribute("Appearance")]
		public int MaximumPerPage
		{
			get { return _MaxPerPage == 0 ? (BlogSettings.Instance.AlbumColumns * BlogSettings.Instance.AlbumRows) : _MaxPerPage; }
			set { _MaxPerPage = value; }
		}

		private int _MaxShowable = 0;
		[BindableAttribute(true)]
		[CategoryAttribute("Appearance")]
		public int MaximumShowable
		{
			get { return _MaxShowable; }
			set { _MaxShowable = value; }
		}

		private Guid _AlbumId = Guid.Empty;
		[BindableAttribute(true)]
		[CategoryAttribute("Appearance")]
		public Guid AlbumId
		{
			get { return _AlbumId; }
			set { _AlbumId = value; }
		}

		private string _AlbumSlug = string.Empty;
		[BindableAttribute(true)]
		[CategoryAttribute("Appearance")]
		public string AlbumSlug
		{
			get { return _AlbumSlug; }
			set { _AlbumSlug = value; }
		}

		private string _TagString = string.Empty;
		[BindableAttribute(true)]
		[CategoryAttribute("Appearance")]
		public string TagString
		{
			get { return _TagString; }
			set { _TagString = value; }
		}

		/// <summary>
		/// use the photo object to render a standard photo image
		/// </summary>
		/// <param name="writer"></param>
		protected override void Render(HtmlTextWriter writer)
        {
            //make sure we have a collection of photos to render.
            if (_AlbumId != Guid.Empty)
            {
                FillPhotoList(Album.GetById(_AlbumId));
            }
            else if (!string.IsNullOrEmpty(_AlbumSlug))
            {
                Album album = Album.GetBySlug(_AlbumSlug);
                if (album != null)
                {
                    FillPhotoList(album);
                }
            }
            else
            {
                FillPhotoList(_TagString);
            }

            if (_PhotoList.Count > 0)
            {
                using (System.IO.StringWriter sw = new System.IO.StringWriter())
                {
                    writer.Write("<ul class=\"photoalbum\">");

                    using (HtmlTextWriter myWriter = new HtmlTextWriter(sw))
                    {
                        foreach (Photo photo in _PhotoList)
                        {
                            RenderPhoto(photo, myWriter);
                        }
                    }

                    writer.Write(sw.ToString());
                    writer.Write("</ul>");
                }
            }
        }

		/// <summary>
		/// Tries three times to get a random visible image
		/// </summary>
		/// <returns></returns>
		private void RenderPhoto(Photo photo, HtmlTextWriter writer)
		{
			writer.Write("<li class=\"photoalbumitem\">");

			DisplayPhoto mPhoto = new DisplayPhoto();
			mPhoto.PhotoId = photo.Id;
			mPhoto.Slug = string.Empty;
			mPhoto.Size = MaximumPhotoSize;
			mPhoto.RelTag = _RelTag.ToString();
			mPhoto.Inline = false;
			mPhoto.RenderControl(writer);

			writer.Write("</li>");
		}

		private void FillPhotoList(Album album)
		{
			_PhotoList = Photo.GetByAlbum(album);
		}

		private void FillPhotoList(string tags)
		{

		}
	}
}